<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="轮播类型" prop="carouselType">
        <el-select v-model="queryParams.carouselType" placeholder="请选择轮播类型" clearable>
          <el-option
            v-for="dict in options.carouselType"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="标题" align="center" prop="titleInfo"/>
      <el-table-column label="轮播类型" align="center" prop="carouselType">
        <template slot-scope="scope">
          {{ getLabelByDictValue(scope.row.carouselType, options.carouselType) }}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="拓展字段" align="center" prop="expandStr"/>
      <el-table-column label="轮播图片" align="center" prop="imaheUrl">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 50px" :src="scope.row.imaheUrl"
                    :preview-src-list="[scope.row.imaheUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="新跳转地址" align="center" prop="newHref"/>
      <el-table-column label="老跳转地址" align="center" prop="oldHref"/>
      <el-table-column label="添加人" align="center" prop="addUser"/>
      <el-table-column label="添加时间" align="center" prop="addDate"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <CreateDialog ref="createDialog" :carouselType="options.carouselType" @submit="getList" />
  </div>
</template>

<script>
import {pageList, deleteById} from "@/api/system/carousel";
import {getDictData} from "@/api/system/dict/data";
import CreateDialog from './create'

export default {
  name: "carousel",
  components:{
    CreateDialog
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carouselType: undefined,
      },
      options: {
        carouselType: []
      },
    };
  },
  created() {
    this.getOptions();
    this.getList();
  },
  methods: {
    /** 获取下拉选项 */
    getOptions() {
      getDictData('carousel_type').then(res => {
        this.options.carouselType = res.data
      })
    },
    /** 查询类型列表 */
    getList() {
      this.loading = true;
      pageList(
        this.queryParams
      ).then(response => {
          this.tableData = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.createDialog.open()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.createDialog.open(row)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除？').then(function () {
        return deleteById(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 获取字典标签
    getLabelByDictValue(value, options) {
      let item = options.find(i => String(i.dictValue) === String(value)) || {}
      return item.dictLabel
    },
  }
};
</script>
